document.addEventListener('DOMContentLoaded', function() {
    const buttons = {
        fullPageScreenshot: document.getElementById('fullPageScreenshot'),
        screenshot: document.getElementById('screenshot'),
        savePDF: document.getElementById('savePDF'),
        saveWord: document.getElementById('saveWord'),
        saveExcel: document.getElementById('saveExcel')
    };

    const progress = document.getElementById('progress');
    const progressText = document.getElementById('progressText');
    const error = document.getElementById('error');
    const errorText = document.getElementById('errorText');
    const success = document.getElementById('success');
    const successText = document.getElementById('successText');

    // 显示进度
    function showProgress(message) {
        progress.classList.add('show');
        progressText.textContent = message;
        disableAllButtons();
    }

    // 隐藏进度
    function hideProgress() {
        progress.classList.remove('show');
        enableAllButtons();
    }

    // 显示错误
    function showError(message) {
        error.classList.add('show');
        errorText.textContent = message;
        setTimeout(() => {
            error.classList.remove('show');
        }, 3000);
    }

    // 显示成功
    function showSuccess(message) {
        success.classList.add('show');
        successText.textContent = message;
        setTimeout(() => {
            success.classList.remove('show');
        }, 3000);
    }

    // 禁用所有按钮
    function disableAllButtons() {
        Object.values(buttons).forEach(button => {
            button.disabled = true;
        });
    }

    // 启用所有按钮
    function enableAllButtons() {
        Object.values(buttons).forEach(button => {
            button.disabled = false;
        });
    }

    // 发送消息到content script并处理响应
    async function sendMessage(action) {
        try {
            const [tab] = await chrome.tabs.query({active: true, currentWindow: true});
            
            if (!tab) {
                showError('无法获取当前标签页');
                return;
            }

            const messages = {
                fullPageScreenshot: '正在生成长截图...',
                screenshot: '正在生成截图...',
                savePDF: '正在转换为PDF...',
                saveWord: '正在转换为Word...',
                saveExcel: '正在转换为Excel...'
            };

            showProgress(messages[action]);

            await chrome.tabs.sendMessage(tab.id, {action});
            
            const successMessages = {
                fullPageScreenshot: '长截图已保存',
                screenshot: '截图已保存',
                savePDF: 'PDF文件已生成',
                saveWord: 'Word文档已生成',
                saveExcel: 'Excel文件已生成'
            };

            showSuccess(successMessages[action]);
        } catch (error) {
            console.error('操作失败:', error);
            showError('操作失败，请重试');
        } finally {
            hideProgress();
        }
    }

    // 绑定按钮点击事件
    Object.entries(buttons).forEach(([action, button]) => {
        button.addEventListener('click', () => sendMessage(action));
    });
}); 